package com.bricks2break;

import sheep.collision.CollisionLayer;
import sheep.game.Sprite;
import sheep.game.State;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.events.EventTimer;
import com.screens.GameOver;
import com.sprites.Ball;
import com.sprites.BrickGroup;
import com.sprites.Paddle;

/**
 * 
 * This class handles everything that has to do with the game play.
 * 
 */
public class Level extends State {

	/**
	 * Creates the objects.
	 */
	private Player playerBottom, playerTop;
	private Paddle paddle1;
	private Paddle paddle2;
	private static Paint black = new Paint();
	private Ball ball1, ball2;
	private CollisionLayer collisionLayer;
	private int lvlNumber = 0;

	/**
	 * Constructor.
	 * 
	 * @param lvlNumber
	 */
	public Level(int lvlNumber) {
		// Setup
		this.lvlNumber = lvlNumber;
		Statics.level = this;
		collisionLayer = new CollisionLayer();
		Statics.eventTimer = new EventTimer();
		Statics.brickGroup = new BrickGroup(lvlNumber);

		// Create objects/sprites
		paddle1 = new Paddle(Statics.POSITION_PADDLE_BOTTOM);
		paddle2 = new Paddle(Statics.POSITION_PADDLE_TOP);
		playerBottom = new Player(paddle1);
		playerTop = new Player(paddle2);
		ball1 = new Ball(Statics.POSITION_BRICK_SPAWN_BOTTOM);
		ball2 = new Ball(Statics.POSITION_BRICK_SPAWN_TOP);

		// Add Collision Layer
		collisionLayer.addSprite(ball1);
		collisionLayer.addSprite(ball2);
		collisionLayer.addSprite(paddle1);
		collisionLayer.addSprite(paddle2);

		// Add touchlisteren for screen
		addTouchListener(this);

		// set textsize to 40
		black.setTextSize(40);
		Statics.txt.setTextSize(20);
	}

	public CollisionLayer getCollisionLayer() {
		return collisionLayer;
	}

	/**
	 * Draw method.
	 * 
	 * @param canvas
	 */
	public void draw(android.graphics.Canvas canvas) {
		// Draw sprites
		canvas.drawRect(0, 0, Statics.SCREEN_WIDTH, Statics.SCREEN_HEIGHT, Statics.black);
		paddle1.draw(canvas);
		paddle2.draw(canvas);
		Statics.brickGroup.draw(canvas);
		ball1.draw(canvas);
		ball2.draw(canvas);

		// Draw score
		canvas.drawText(Integer.toString(playerTop.getScore().getScoreValue()), Statics.SCREEN_WIDTH - 50, 60, Statics.txt);
		canvas.drawText(Integer.toString(playerBottom.getScore().getScoreValue()), Statics.SCREEN_WIDTH - 50, Statics.SCREEN_HEIGHT - 50, Statics.txt);
	}

	/**
	 * Update method
	 * 
	 * @param dt
	 */
	public void update(float dt) {
		// All updates
		collisionLayer.update(dt);
		paddle1.update(dt);
		paddle2.update(dt);
		ball1.update(dt);
		ball2.update(dt);
		Statics.brickGroup.update(dt);
		Statics.eventTimer.update(dt);
	}

	/**
	 * @param event
	 */
	public synchronized boolean onTouchDown(MotionEvent event) {
		return true;
	}

	/**
	 * Move paddle
	 * 
	 * @param event
	 */
	public synchronized boolean onTouchMove(MotionEvent event) {
		for (int i = 0; i < event.getPointerCount(); i++) {
			if (paddle1.isUnderTouch((int) event.getX(i), (int) event.getY(i))) {
				paddle1.setPositionX(event.getX(i));
			}
			if (paddle2.isUnderTouch((int) event.getX(i), (int) event.getY(i))) {
				paddle2.setPositionX(event.getX(i));
			}
		}
		return true;
	}

	/**
	 * @param event
	 */
	public synchronized boolean onTouchUp(MotionEvent event) {
		return true;
	}

	/**
	 * Handles what happens when the game/level is over. Opens the game over
	 * screen.
	 */
	public void gameOver() {
		String winnerTxt = " ";
		String loserTxt = " ";
		// Check who the winner is, and number of points to each player
		// Tied Game
		if (playerBottom.getScore().getScoreValue() == playerTop.getScore().getScoreValue()) {
			winnerTxt = Statics.string_game_tied;
			loserTxt = "They both got " + playerBottom.getScore().getScoreValue() + " points.";
		}
		// Top player wins
		else if (playerTop.getScore().getScoreValue() > playerBottom.getScore().getScoreValue()) {
			winnerTxt = Statics.string_winner_top + playerTop.getScore().getScoreValue() + " points.";
			loserTxt = Statics.string_loser_bottom + playerBottom.getScore().getScoreValue() + " points.";
		}
		// Bottom player wins
		else {
			winnerTxt = Statics.string_winner_bottom + playerBottom.getScore().getScoreValue() + " points.";
			loserTxt = Statics.string_loser_top + playerTop.getScore().getScoreValue() + " points.";
		}
		// Switch to gameOver screen
		getGame().popState();
		getGame().pushState(new GameOver(winnerTxt, loserTxt, lvlNumber));
	}

	public Player getTopPlayer() {
		return playerTop;
	}

	public Player getBottomPlayer() {
		return playerBottom;
	}
}
